<template>
  <OrderSeePage :backname="backname" :orderTitle="orderTitle">
    <template v-slot:OrderDetail>
      <li>
        订单号：<span>{{ info.order_sn }}</span>
      </li>
      <li>
        用户名称：<span>{{ info.user_name }}</span>
      </li>
      <li>
        下单日期：<span>{{
          parseTime(Number(info.created_at * 1000), "{y}-{m}-{d}")
        }}</span>
      </li>
      <li>
        配送日期：<span
          >{{ parseTime(info.send_time, "{y}-{m}-{d}") }}
          {{ send_time[info.send_time_id] }}
        </span>
      </li>
      <li v-if="show_original_order_data">
        原下单金额：<span>{{ info.original_should_money }}</span>
      </li>
      <li>
        下单金额：<span>{{ total.info_order_money }}</span>
      </li>
      <li v-if="show_original_order_data">
        原配送金额：<span>{{ info.send_money }}</span>
      </li>
      <li>
        配送金额：<span>{{ total.info_sort_money }}</span>
      </li>
       <li>
        原订单备注：<span>{{ info.remark?info.remark:"空" }}</span>
      </li>
      <li>
        运费：<span>
          <el-input-number
            placeholder="运费"
            v-model.trim="info.delivery_fee"
            :min="0"
            :precision="2"
            :controls="false"
            @change="isEdit = true"
          ></el-input-number>
        </span>
      </li>
      <li>
        优惠金额：<span>
          <el-input-number
            placeholder="优惠金额"
            v-model.trim="info.discount_money"
            :min="0"
            :precision="2"
            :controls="false"
            @change="isEdit = true"
          ></el-input-number>
        </span>
      </li>
      <!-- <li class='main_w100'> -->
        <!-- <add-input @confirm="handleConfirm" ref="addInput"></add-input> -->
      <!-- </li> -->
    </template>
    <template v-slot:BackNameRight>
      <el-button @click="handleBatch">批量处理</el-button>
      <el-button @click="handleReset" class="reset" >重置</el-button>
    </template>
    <template v-slot:default>
      <Table>
        <template v-slot:Title>
          商品信息
        </template>
        <template v-slot:Table>
          <add-input style='margin-bottom:10px;font-size:14px' @confirm="handleConfirm" ref="addInput"></add-input>
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list"
            element-loading-text="加载中"
            fit
            border
            highlight-current-row
            @selection-change="handleSelectionChange"
            show-summary
            :summary-method="getSummaries"
          >
            <el-table-column width="60" type="selection" />
            <el-table-column width="60" label="序号" prop="userId">
              <template slot-scope="scope">{{ scope.$index + 1 }}</template>
            </el-table-column>
            <el-table-column
              label="商品名称"
              prop="name"
              show-overflow-tooltip
              min-width="130"
            />
            <el-table-column label="单位" prop="unit" show-overflow-tooltip />
            <el-table-column
              label="下单数量"
              :show-overflow-tooltip="true"
              class-name="main_hei"
              min-width="130"
            >
              <template slot-scope="scope">
                <table-edit-input
                  :number="scope.row.order_num"
                  :is_standard="scope.row.goods.is_standard"
                  @get_data="val => get_data(val, scope, 1)"
                ></table-edit-input>
              </template>
            </el-table-column>
            <el-table-column
              label="下单单价"
              :show-overflow-tooltip="true"
              class-name="main_hei"
              min-width="130"
            >
              <template slot-scope="scope">
                <table-edit-input
                  :number="scope.row.order_price"
                  @get_data="val => get_data(val, scope, 2)"
                ></table-edit-input>
              </template>
            </el-table-column>
            <el-table-column
              label="下单金额"
              :show-overflow-tooltip="true"
              class-name="main_hei"
              min-width="130"
            >
                  <!-- scope.row.order_money -->
              <template slot-scope="scope">
                {{
                  roundFloat(
                    multiplyFloat(scope.row.order_num, scope.row.order_price),
                    2
                  )
                }}
              </template>
            </el-table-column>
            <el-table-column
              label="配送数量"
              :show-overflow-tooltip="true"
              class-name="main_hei"
              min-width="130"
            >
              <template slot-scope="scope">
                <table-edit-input
                  :number="scope.row.sort_num"
                  :is_standard="scope.row.goods.is_standard"
                  @get_data="val => get_data(val, scope, 3)"
                ></table-edit-input>
              </template>
            </el-table-column>
            <el-table-column
              label="配送单价"
              :show-overflow-tooltip="true"
              class-name="main_hei"
              min-width="130"
            >
              <template slot-scope="scope">
                <table-edit-input
                  :number="scope.row.sort_price"
                  @get_data="val => get_data(val, scope, 4)"
                ></table-edit-input>
              </template>
            </el-table-column>
            <el-table-column
              label="配送金额"
              :show-overflow-tooltip="true"
              class-name="main_hei"
              min-width="130"
            >
                  <!-- scope.row.sort_money -->
              <template slot-scope="scope">
                {{
                  roundFloat(
                    multiplyFloat(scope.row.sort_price, scope.row.sort_num),
                    2
                  )
                }}
              </template>
            </el-table-column>

            <el-table-column
              label="备注"
              :show-overflow-tooltip="true"
              prop="remark"
              class-name="main_hei"
            />

            <el-table-column
              label="管理操作"
              align="center"
              fixed="right"
              width="150"
              class-name="main_hei handle"
              class="handle"
            >
              <template slot-scope="scope">
                <el-dropdown v-if="show_original_order_data">
                  <span class="el-dropdown-link" style="cursor: pointer">
                    原数据
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>
                      <el-link style="color: rgb(17, 138, 178)"
                        >原下单数量：{{ scope.row.original_order_num }}</el-link
                      >
                    </el-dropdown-item>
                    <el-dropdown-item>
                      <el-link style="color: rgb(17, 138, 178)"
                        >原下单单价：{{
                          scope.row.original_order_price
                        }}</el-link
                      >
                    </el-dropdown-item>
                    <el-dropdown-item>
                      <el-link style="color: rgb(17, 138, 178)"
                        >原下单金额：{{
                          scope.row.original_order_money
                        }}</el-link
                      >
                    </el-dropdown-item>
                    <el-dropdown-item>
                      <el-link style="color: rgb(17, 138, 178)"
                        >原配送数量：{{ scope.row.original_sort_num }}</el-link
                      >
                    </el-dropdown-item>
                    <el-dropdown-item>
                      <el-link style="color: rgb(17, 138, 178)"
                        >原配送单价：{{
                          scope.row.original_sort_price
                        }}</el-link
                      >
                    </el-dropdown-item>
                    <el-dropdown-item>
                      <el-link style="color: rgb(17, 138, 178)"
                        >原配送金额：{{
                          scope.row.original_sort_money
                        }}</el-link
                      >
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
                <el-link :type="list.length > 1 ? 'danger' : 'info'" :disabled="list.length <= 1" @click="handleDel(scope.row,scope.$index)">删除</el-link>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </Table>
      <div class="newConBox_p200 paddingBottom10" id="newConBox">
        <div class="newConBox_h32">
          <div :class="['mainor_pr boxdq newConBox ', { on: searchBarFixed }]">
            <div class="mainor_prl">
              <el-button
                type="primary"
                :loading="btn_loading"
                @click="preservation"
                v-repeatClick
                >保存</el-button
              >

              <el-button @click="$router.back(-1)">取消</el-button>
            </div>
          </div>
        </div>
      </div>
    </template>
    <template v-slot:Dialog
      ><el-dialog
        title="批量处理"
        :visible.sync="dialogVisible"
        @closed="closeDialog"
        width="530px"
      >
        <div>
          <el-form ref="form" :model="form" label-width="130px">
            <el-form-item label="总金额：">
              <el-radio-group v-model="form.total_money" @change="val => changeType(val, 1)">
                <el-radio :label="1">增加</el-radio>
                <el-radio :label="2">减少</el-radio>
                <el-radio :label="3">不变</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item>
              <span slot="label"
                >修改对象
                <el-tooltip class="item" effect="dark" placement="bottom">
                  <div slot="content">
                    注释：总金额不变动情况下，修改数量或单价，<br />相对应的数量或单价会随之改变
                  </div>
                  <svg
                    aria-hidden="true"
                    class="navbar_svg new_navbar_svg"
                    width="14"
                    height="14"
                  >
                    <use xlink:href="#icon-summary_question" />
                  </svg> </el-tooltip
                >：</span
              >
              <el-radio-group
                v-model="form.change_object"
                @change="val => changeType(val, 2)"
              >
                <el-radio :label="1">数量</el-radio>
                <el-radio :label="2">单价</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item>
              <span slot="label">保留两位小数
                <el-tooltip class="item" effect="dark" placement="bottom">
                  <div slot="content">
                    注释：根据修改对象中选中的对象来选择保留两位小数方式<br />
                  </div>
                  <svg
                    aria-hidden="true"
                    class="navbar_svg new_navbar_svg"
                    width="14"
                    height="14"
                  >
                    <use xlink:href="#icon-summary_question" />
                  </svg> 
                </el-tooltip>：
              </span>
              <el-radio-group v-model="form.keepDecimals">
                <el-radio :label="1">四舍五入</el-radio>
                <el-radio :label="2">截取</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="修改方式：">
              <el-radio-group
                v-model="form.change_type"
                @change="val => changeType(val, 3)"
              >
                <el-radio :label="1">金额</el-radio>
                <el-radio :label="2">比例</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item>
              <span slot="label">数值：</span>
              <el-input-number
                v-show="form.change_type == 1"
                v-model.trim="form.money"
                :precision="2"
                :controls="false"
                :step="2"
                :min="0"
              ></el-input-number>

              <el-input-number
                v-show="form.change_type == 2"
                v-model.trim="form.scale"
                :min="number_min"
                :max="0.99"
                :precision="2"
                :step="0.01"
              ></el-input-number>
              
              <el-tooltip class="item" effect="dark" placement="bottom">
                <div slot="content">
                  注释：总金额不变，比例修改范围 -0.99至0.99<br />
                  总金额增加或减少，比例修改范围 0.01至0.99
                </div>
                <svg
                  aria-hidden="true"
                  class="navbar_svg new_navbar_svg"
                  width="14"
                  height="14"
                  v-show="form.change_type == 2"
                >
                  <use xlink:href="#icon-summary_question" />
                </svg>
              </el-tooltip>
            </el-form-item>
          </el-form>
        </div>
        <textInfo :info="form" :length="selectArr.length"></textInfo>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleComfirm">确 定</el-button>
        </span>
      </el-dialog>
    </template>
  </OrderSeePage>
</template>
<script>
import OrderSeePage from "@/components/OrderSeePage";
import Table from "@/components/OrderSeePage/table.vue";
import tableEditInput from "./components/tableEditInput.vue";
import addInput from './components/addInput.vue'
import textInfo from './components/textInfo.vue'
import {
  parseTime,
  jsonStringifyBack,
  exportMethod,
  getNowMonth
} from "@/utils/index";
import { multiplyFloat, roundFloat } from "@/utils/math.js";
import {
  get_empty_info,
  batch_unbind_account,
  batchCalcuate,
  changeTableData
} from "@/api/financial/finaicalOther";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { mapGetters } from "vuex";
export default {
  components: {
    OrderSeePage,
    Table,
    tableEditInput,
    addInput,
    textInfo
  },
  data() {
    return {
      parseTime,
      multiplyFloat,
      roundFloat,
      exportMethod,
      backname: "其他用户对账",
      orderTitle: "订单信息",
      listLoading: false,
      // 监听滚动
      searchBarFixed: false,
      info: {},
      original_info: {},
      logs: [],
      send_time: [],
      id: "",
      list: [],
      original_list: [],
      btn_loading: false,
      // 批量处理
      dialogVisible: false,
      form: {
        total_money: 1, //总金额  1增加 2减少  3不变
        change_object: 1, //修改对象 1 数量  2 单价
        change_type: 1, //修改方式  1 金额  2 比例
        money: 0,
        scale: 0.01,
        keepDecimals: 1 //  保留两位小数  1四舍五入 2截取
      },
      isEdit: false,
      selectArr: [],
      deletedIds:[],
    };
  },
  computed: {
    total() {
      let order_money = 0;
      let sort_money = 0;
      let info_order_money = 0;
      let info_sort_money = 0;
      let order = 0;
      let sort = 0;
      this.list.map(e => {
        order = this.roundFloat(
          this.multiplyFloat(e.order_num, e.order_price),
          2
        );
        sort = this.roundFloat(this.multiplyFloat(e.sort_price, e.sort_num), 2);
        order_money += parseFloat(order);
        sort_money += parseFloat(sort);
      });
      info_order_money =
        order_money +
        parseFloat(this.info.delivery_fee) -
        parseFloat(this.info.discount_money);
      info_sort_money =
        sort_money +
        parseFloat(this.info.delivery_fee) -
        parseFloat(this.info.discount_money);
      order_money = this.roundFloat(order_money, 2);
      sort_money = this.roundFloat(sort_money, 2);
      info_order_money = this.roundFloat(info_order_money, 2);
      info_sort_money = this.roundFloat(info_sort_money, 2);
      console.log(order_money, sort_money, info_order_money, info_sort_money);
      return {
        order_money,
        sort_money,
        info_order_money,
        info_sort_money
      };
    },
    number_min() {
      if (this.form.total_money == 3) {
        return -0.99;
      } else {
        return 0.01;
      }
    },
    ...mapGetters({
      show_original_order_data: "show_original_order_data"
    })
  },
  methods: {
    handleDel(row,index){
      if(row.id){
        this.deletedIds.push(row.id)
      }
      this.list.splice(index,1)
    },
    handleConfirm({selected,num}){

      if(this.list.findIndex(item=>item.goods_spu_id == selected.goods_spu_id) != -1){
        this.$message.warning('商品已存在')
        return
      }

      selected.order_num = num
      selected.unit = selected.spu_unit.title
      selected.sort_num = num
      selected.sort_price = selected.order_price
      selected.original_order_num = 0.00
      selected.original_order_price = 0.00
      selected.original_order_money = 0.00
      selected.original_sort_num = 0.00
      selected.original_sort_price = 0.00
      selected.original_sort_money = 0.00
      this.list.push(selected)
    },
    get_empty_info() {
      
      let data = {}
      if(this.$route.query.type == 0){
        data.info_id = this.id
      }else{
        data.account_id = this.id
      }
      
      this.listLoading = true;
      get_empty_info(data).then(res => {
        if (res.errcode == 0) {
          let info = null;
          let original_info = null;
          let original_list = null;
          if (res.data.account_info) {
            info = res.data.account_info;
            info.order_list && info.order_list.map(e => {
              e.original_order_money = this.roundFloat(
                this.multiplyFloat(
                  e.original_order_num,
                  e.original_order_price
                ),
                2
              );
              e.original_sort_money = this.roundFloat(
                this.multiplyFloat(e.original_sort_num, e.original_sort_price),
                2
              );
            });
            original_info = this.handleNoAccountInfo(res.data.account_info || res.data.info);
            original_info.user_name = "";
            if (original_info.zid_info) {
              original_info.user_name = `${original_info.member.nickname}【${
                original_info.zid_info.nickname
              }】`;
            } else {
              original_info.user_name = `${original_info.member.nickname}`;
            }
            original_list = original_info.order_list;
          } else {
            info = this.handleNoAccountInfo(res.data.info);
            delete info.id
          }
          info.user_name = "";
          if (info.zid_info) {
            info.user_name = `${info.member.nickname}【${
              info.zid_info.nickname
            }】`;
          } else {
            info.user_name = `${info.member.nickname}`;
          }
          
          this.info = info;
          this.$refs.addInput.setData(info)
          this.send_time = res.data.send_time;

          info.order_list && info.order_list.forEach(item=>{
            item.name = item.name + `【${item.unit}】`
          })

          this.list = info.order_list.map(el => {
            el.order_money = el.original_order_money
            el.sort_money = el.original_sort_money
            return el
          });
          // console.log(this.list, 1111);
          if (res.data.account_info) {
            this.original_info = Object.assign({}, original_info);
            this.original_list = JSON.parse(JSON.stringify(original_list));
          } else {
            this.original_info = Object.assign({}, this.info);
            this.original_list = JSON.parse(JSON.stringify(info.order_list));
          }

          this.logs = res.data.logs;
        } else {
          this.$message.error(res.errmsg);
        }
        this.listLoading = false;
      });
    },
    // 处理未对账原始数据
    handleNoAccountInfo(info) {
      info.original_send_money = info.original_send_money || info.send_money;
      info.original_should_money = info.original_should_money || info.should_money;
      info.order_list && info.order_list.map(e => {
        e.original_order_num = e.original_order_num || e.order_num;
        e.original_order_price = e.original_order_price || e.order_price;
        e.original_order_money = this.roundFloat(
          this.multiplyFloat(e.original_order_num, e.original_order_price),
          2
        );

        e.original_sort_num = e.original_sort_num || e.sort_num;
        e.original_sort_price = e.original_sort_price || e.sort_price;
        e.original_sort_money = this.roundFloat(
          this.multiplyFloat(e.original_sort_num, e.original_sort_price),
          2
        );
      });
      return info;
    },
    // 表格子组件传值
    get_data(data, scope, type) {
      let row = scope.row;
      this.isEdit = true;
      switch (type) {
        case 1:
          row.order_num = data;
          break;
        case 2:
          row.order_price = data;
          break;
        case 3:
          row.sort_num = data;
          break;
        case 4:
          row.sort_price = data;
          break;
      }
      this.list.splice(scope.$index,1)
      this.list.splice(scope.$index,0,row)
      // let params = null
      // if([1, 2].includes(type)) {
      //   params = {
      //     value_1: scope.row.order_num,
      //     value_2: scope.row.order_price,
      //     noun: 3,
      //     percent: 2
      //   }
      // }else {
      //   params = {
      //     value_1: scope.row.sort_num,
      //     value_2: scope.row.sort_price,
      //     noun: 3,
      //     percent: 2
      //   }
      // }
      // changeTableData(this.packageApi(params)).then(({ data }) => {
      //   if([1, 2].includes(type)) {
      //     row.order_money = data.res
      //   }else {
      //     row.sort_money = data.res
      //   }
      // })
    },
    // 批量
    handleBatch() {
      if (this.selectArr.length == 0) {
        return this.$message.warning("请勾选商品");
      }
      this.dialogVisible = !this.dialogVisible;
    },
    // 重置
    handleReset() {
      this.$confirm("确定重置回原订单数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.info = Object.assign({}, this.original_info);

          this.list = JSON.parse(JSON.stringify(this.original_list));
        })
        .catch(() => {});
    },
    // 关闭弹窗
    closeDialog() {
      this.form.total_money = 1;
      this.form.change_object = 1;
      this.form.change_type = 1;
      this.form.money = 0;
      this.form.scale = 0.01;
    },
    // 批量选择
    handleSelectionChange(val) {
      this.selectArr = val;
    },
    packageApi(data = {}) {
      let query = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        data
      );
      return query;
    },
    handleComfirm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.isEdit = true;
          // this.handleBatchData();
          // this.$nextTick(() => {
          //   this.dialogVisible = false;
          // });
          const totalList = [1, -1, 0]
          let newObj = {
            data: JSON.stringify(
              this.selectArr.map(el => {
                return {
                  order_list_id: el.id,
                  order_price: el.order_price,
                  order_num: el.order_num,
                  order_amount: this.roundFloat( this.multiplyFloat(el.order_num, el.order_price), 2),
                  send_num: el.sort_num,
                  send_price: el.sort_price,
                  send_amount: this.roundFloat(this.multiplyFloat(el.sort_price, el.sort_num), 2)
                }
              })
            ),
            total_amount: totalList[this.form.total_money - 1],
            update_object: this.form.change_object,
            update_method: this.form.change_type,
            two_decimal: this.form.keepDecimals,
            value: this.form.change_type === 1 ? this.form.money : this.form.scale
          }
          this.packageApi(newObj)
          batchCalcuate(newObj).then(({ data }) => {
            if(data.length) {
              this.list.forEach(el => {
                data.forEach(item => {
                  if(el.id === item.order_list_id) {
                    el.order_num = item.order_num
                    el.order_price = item.order_price
                    el.sort_num = item.send_num
                    el.sort_price = item.send_price
                    el.order_money = item.order_amount
                    el.sort_money = item.send_amount
                  }
                })
              })
              this.dialogVisible = false;
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 批量数据处理
    handleBatchData() {
      let { order_money_all, sort_money_all } = this.handleListData();
      let order_modify_percent = 0;
      let sort_modify_percent = 0;
      if (this.form.change_type == 2) {
        order_modify_percent = this.form.scale;
        sort_modify_percent = this.form.scale;
      } else if (this.form.change_type == 1) {
        order_modify_percent = scale(
          order_money_all,
          this.form.money,
          this.form.total_money,
          this.form.keepDecimals
        );
        sort_modify_percent = scale(
          sort_money_all,
          this.form.money,
          this.form.total_money,
          this.form.keepDecimals
        );
      }
      this.selectArr.map(e => {
        e = this.dealWithData(e, order_modify_percent, sort_modify_percent);
      })
      function scale(n, e, total_money, keepDecimals) {
        n = parseFloat(n);
        e = parseFloat(e);
        n = e / n;
        const re = /([0-9]+\.[0-9]{2})[0-9]*/
        if (total_money == 1) {
          return keepDecimals === 1 ? +n.toFixed(2) : n.toString().replace(re, "$1");
        } else if (total_money == 2) {
          return keepDecimals === 1 ? -n.toFixed(2) : n.toString().replace(re, "$1");
        }
      }
      // console.log(this.list, 111);
    },
    // 处理详细的数据
    dealWithData(data, order_modify_percent, sort_modify_percent) {
      order_modify_percent = parseFloat(order_modify_percent);
      sort_modify_percent = parseFloat(sort_modify_percent);
      let a1 = parseFloat(data.order_num);
      let b1 = parseFloat(data.order_price);
      let c1 = parseFloat(data.order_money);
      let a2 = parseFloat(data.sort_num);
      let b2 = parseFloat(data.sort_price);
      let c2 = parseFloat(data.sort_money);
      let is_standard = data.goods.is_standard;
      let form = this.form;
      let type = null;
      // type 1  增加减少   2不变
      if (form.total_money == 3) {
        type = 2;
      } else {
        type = 1;
      }
      switch (type) {
        case 1:
          if (form.change_object == 1) {
            data.order_num =
              c1 != 0
                ? is_standard == 1
                  ? parseFloat(a1 + (c1 * order_modify_percent) / b1).toFixed(0)
                  : parseFloat(a1 + (c1 * order_modify_percent) / b1).toFixed(2)
                : 0;
            data.sort_num =
              c2 != 0
                ? is_standard == 1
                  ? parseFloat(a2 + (c2 * sort_modify_percent) / b2).toFixed(0)
                  : parseFloat(a2 + (c2 * sort_modify_percent) / b2).toFixed(2)
                : 0;
          } else if (form.change_object == 2) {
            data.order_price =
              c1 != 0
                ? parseFloat(b1 + (c1 * order_modify_percent) / a1).toFixed(2)
                : 0;
            data.sort_price =
              c2 != 0
                ? parseFloat(b2 + (c2 * sort_modify_percent) / b2).toFixed(2)
                : 0;
          }
          break;
        case 2:
          if (form.change_object == 1) {
            data.order_num =
              is_standard == 1
                ? parseFloat(a1 * (1 + order_modify_percent)).toFixed(0)
                : parseFloat(a1 * (1 + order_modify_percent)).toFixed(2);
            data.order_price =
              c1 != 0 ? parseFloat(c1 / data.order_num).toFixed(2) : 0;
            data.sort_num =
              is_standard == 1
                ? parseFloat(a2 * (1 + sort_modify_percent)).toFixed(0)
                : parseFloat(a2 * (1 + sort_modify_percent)).toFixed(2);
            data.sort_price =
              c2 != 0 ? parseFloat(c2 / data.sort_num).toFixed(2) : 0;
          } else if (form.change_object == 2) {
            data.order_price = parseFloat(
              b1 * (1 + order_modify_percent)
            ).toFixed(2);
            data.order_num =
              c1 != 0
                ? is_standard == 1
                  ? parseFloat(c1 / data.order_price).toFixed(0)
                  : parseFloat(c1 / data.order_price).toFixed(2)
                : 0;
            data.sort_price = parseFloat(
              b2 * (1 + sort_modify_percent)
            ).toFixed(2);
            data.sort_num =
              c2 != 0
                ? is_standard == 1
                  ? parseFloat(c2 / data.sort_price).toFixed(0)
                  : parseFloat(c2 / data.sort_price).toFixed(2)
                : 0;
          }
          break;
      }
      return data;
    },
    // 列表数据处理
    handleListData() {
      let order_money_all = 0;
      let sort_money_all = 0;
      this.selectArr.map(e => {
        e.order_money = roundFloat(
          multiplyFloat(e.order_num, e.order_price),
          2
        );
        e.sort_money = roundFloat(multiplyFloat(e.sort_price, e.sort_num), 2);
        order_money_all += parseFloat(e.order_money);
        sort_money_all += parseFloat(e.sort_money);
      });
      return { order_money_all, sort_money_all };
    },
    // 保存
    preservation() {
      this.btn_loading = true;
      let list = JSON.parse(JSON.stringify(this.list));
      let arry = [];
      let form = {
        // id:this.info.id,
        account_id:this.info.id ? +this.info.id : 0,
        info_id:this.id ? +this.id : 0,
        delivery_fee: this.info.delivery_fee,
        discount_money: this.info.discount_money,
        order_money: this.total.order_money,
        sort_money: this.total.sort_money,
        delete_ids:this.deletedIds.join(",")
      };
      list.map(e => {
        arry.push({
          remark: e.remark,
          sort_num: e.sort_num,
          sort_price: e.sort_price,
          order_price: e.order_price,
        //   roundFloat(
        //   multiplyFloat(e.order_num, e.order_price),
        //   2
        // ),
          order_num: e.order_num,
          id: e.id ? e.id : 0,
          goods_id:e.goods_id,
          goods_spu_id:e.goods_spu_id
        });
      });
      let data = Object.assign({}, form, {
        list: JSON.stringify(arry, jsonStringifyBack)
      });
      batch_unbind_account(data).then(res => {
        this.btn_loading = false;
        if (res.errcode == 0) {
          this.$message.success("对账成功");
          this.isEdit = false;
          this.$router.back(-1);
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 监听滚动
    handleScroll() {
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      scrollTop + document.documentElement.clientHeight >
      document.getElementById("newConBox").offsetTop + 300
        ? (this.searchBarFixed = false)
        : (this.searchBarFixed = true);
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        switch (index) {
          case 0:
            sums[index] = "合计";
            break;
          case 6:
            sums[index] = this.total.order_money;
            break;
          case 9:
            sums[index] = this.total.sort_money;
            break;
        }
      });
      return sums;
    },
    // 批量处理  选择
    changeType(value, type) {
      switch (type) {
        case 1:
          this.form.scale = 0.01;
          if (value == 3) {
            this.form.change_type = 2;
          }
          break;
        case 2:
          break;
        case 3:
          if (value == 1 && this.form.total_money == 3) {
            this.$message.warning("总金额不变，只能选择比例");
            this.$nextTick(() => {
              this.form.change_type = 2;
            });
          }
          break;
      }
    }
  },
  created() {
    this.id = this.$route.params.id;
    setTimeout(()=>{
      localStorage.setItem("accountStatus",this.$route.query.type)
    },100)
    this.get_empty_info();
  },
  beforeRouteLeave(to, from, next) {
    if (this.isEdit) {
      setTimeout(() => {
        this.$confirm(
          "您已经有数据操作，离开页面系统不会保存您的操作，确定离开吗？",
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }
        )
          .then(() => {
            next();
          })
          .catch(() => {
            next(false);
          });
      }, 200);
    } else {
      next();
    }
  },
  mounted() {
    let scrollTop =
      window.pageYOffset ||
      document.documentElement.scrollTop ||
      document.body.scrollTop;
    scrollTop + document.documentElement.clientHeight >
    document.getElementById("newConBox").offsetTop + 300
      ? (this.searchBarFixed = false)
      : (this.searchBarFixed = true);
    window.addEventListener("scroll", this.handleScroll);
  },
  destroyed() {
    window.removeEventListener("scroll", this.handleScroll);
  }
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";
.newConBox.on {
  margin-left: -10px;
}

.newConBox.on {
  z-index: 100 !important;
}
.status1 {
  color: #03a98e;
}
.status0 {
  color: red;
}
.svg_box {
  display: flex;
  flex-direction: column;
  transform: translateY(-2px);
}

.new_navbar_svg {
  vertical-align: middle;
  margin-left: 2px;
  transform: translateY(-1px);
  use {
    fill: #181818;
  }
}
/deep/.el-button--info {
  &.reset {
    border-color: #DCDFE6;
    background: #FFF;
    color: #606266;
    &:hover,
    &:focus {
      border-color: $subActive;
      color: $subActive;
    }
  }
  
}
</style>
